package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@RequestMapping("/admin/order")
@RestController("administratorOrderController")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(Integer page, Integer pageSize, LocalDateTime beginTime,LocalDateTime endTime,String number,String phone,Integer status) throws Exception{
        log.info("订单搜索");
        OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
        ordersPageQueryDTO.setBeginTime(beginTime);
        ordersPageQueryDTO.setEndTime(endTime);
        ordersPageQueryDTO.setPhone(phone);
        ordersPageQueryDTO.setStatus(status);
        ordersPageQueryDTO.setPage(page);
        ordersPageQueryDTO.setPageSize(pageSize);
        ordersPageQueryDTO.setNumber(number);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    //各个状态下的订单数量的统计
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statisticsNumber(){
        log.info("统计各个状态下订单数量的统计");
        OrderStatisticsVO orderStatisticsVO = orderService.getStatics();
        return Result.success(orderStatisticsVO);
    }

    //查询订单详情
    @GetMapping("details/{id}")
    public Result<OrderVO> details(@PathVariable(name = "id") Long id){
        log.info("查询订单详情:{}",id);
        OrderVO orderVO = orderService.getOrderDetail(id);
        return Result.success(orderVO);
    }

    //接单
    @PutMapping("/confirm")
    public Result<Object>confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        log.info("接单,参数:{}",ordersConfirmDTO);
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    //拒单
    @PutMapping("/rejection")
    public Result<Object> rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("拒单,参数:{}",ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    //取消订单
    @PutMapping("/cancel")
    public Result<Object>cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("取消订单:{}",ordersCancelDTO);
        orderService.adminCancel(ordersCancelDTO);
        return Result.success();
    }

    //派送订单
    @PutMapping("/delivery/{id}")
    public Result<Object>delivery(@PathVariable(value = "id") Long id){
        log.info("派送订单:{}",id);
        orderService.delivery(id);
        return Result.success();
    }

    //完成订单
    @PutMapping("/complete/{id}")
    public Result<Object> complete(@PathVariable(value = "id") Long id){
        log.info("完成订单");
        orderService.complete(id);
        return Result.success();
    }
}
